package com.shuiyun.controller;

import com.shuiyun.entity.User;
import com.shuiyun.service.SysUserService;
import com.shuiyun.utils.DataGridResult;
import com.shuiyun.utils.Query;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Resource
    SysUserService sysUserService;
    @RequestMapping("tologin")
    public String toLogin(){

        return "login";
    }

    @RequestMapping("dologin")
    public String dologin(String username, String password, Model model){
        if ("admin".equals(username)&&"123456".equals(password)){
            return "mainframe";
        }else{
            model.addAttribute("msg","用户名或密码错误");
            return "login";
        }
    }

    @RequestMapping("/user/list")
    @ResponseBody
    public DataGridResult findAllMenu(@RequestParam Map<String, Object> map) {
        return sysUserService.findAllUser(new Query(map));
    }

    @RequestMapping("/user/list/nopage")
    @ResponseBody
    public List<User> findAllMenuNoPage() {
        System.out.println("{+++++++++++++"+sysUserService.findAllUserNoPage().get(0).getUserName());
        return sysUserService.findAllUserNoPage();
    }

    @RequestMapping("/user/add")
    @ResponseBody
    public int addUser(@RequestBody User user){
        return sysUserService.add(user);
    }

    @RequestMapping("/user/info/{userId}")
    @ResponseBody
    public User getUserInfo(@PathVariable long userId){
         return sysUserService.selectUserById(userId);
    }

    @RequestMapping("user/update")
    @ResponseBody
    public int updateUser(@RequestBody User user){
        return sysUserService.updateUser(user);
    }

    @RequestMapping("user/del")
    @ResponseBody
    public int delBatch(@RequestBody Long [] ids){
        return sysUserService.deleteBatch(ids);
    }
}
